package com.cadre.common.utils.oldutil;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Base64;

/* loaded from: DesUtilForDataStream.class */
public class DesUtilForDataStream {
    public static final String DEFAULT_KEY = "MFwwDQ==";

    public static String decrypt(String message, String key) throws Exception {
        byte[] bytesrc = convertHexString(message);
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(2, SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key.getBytes("UTF-8"))), new IvParameterSpec(key.getBytes("UTF-8")));
        return new String(cipher.doFinal(bytesrc));
    }

    public static byte[] encrypt(String message, String key) throws Exception {
        Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
        cipher.init(1, SecretKeyFactory.getInstance("DES").generateSecret(new DESKeySpec(key.getBytes("UTF-8"))), new IvParameterSpec(key.getBytes("UTF-8")));
        return cipher.doFinal(message.getBytes("UTF-8"));
    }

    public static byte[] encrypt(String message) throws Exception {
        return encrypt(message, "HXYZDATA");
    }

    public static byte[] convertHexString(String ss) {
        byte[] digest = new byte[ss.length() / 2];
        for (int i = 0; i < digest.length; i++) {
            digest[i] = (byte) Integer.parseInt(ss.substring(2 * i, (2 * i) + 2), 16);
        }
        return digest;
    }

    public static String encrypt2(String message, String key) throws Exception {
        return toHexString(encrypt(URLEncoder.encode(message, "utf-8").toLowerCase(), key)).toUpperCase();
    }

    public static String decrypt2(String message, String key) throws Exception {
        return URLDecoder.decode(decrypt(message, key), "utf-8");
    }

    public static String GetBase64Str(byte[] bs) {
        return Base64.getEncoder().encodeToString(bs);
    }

    public static void main(String[] args) throws Exception {
        System.out.println("加密数据:5C-FF-35-0D-4E-CD");
        System.out.println("后的数加密据为:" + GetBase64Str(encrypt("5C-FF-35-0D-4E-CD", "HXYZDATA")));
    }

    public static String toHexString(byte[] b) {
        StringBuffer hexString = new StringBuffer();
        for (byte b2 : b) {
            String plainText = Integer.toHexString(255 & b2);
            if (plainText.length() < 2) {
                plainText = "0" + plainText;
            }
            hexString.append(plainText);
        }
        return hexString.toString();
    }

    public byte[] encrypt1(String realpwd) throws Exception {
        return encrypt(realpwd, DEFAULT_KEY);
    }
}
